package com.example.abilitydemo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class ServiceAbility extends Ability {
    //private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "ServiceAbility");
    public static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x11100, "ServiceAbility");

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
        super.onStart(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.warn(LABEL_LOG, "ServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.warn(LABEL_LOG, "ServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
        HiLog.info(LABEL_LOG, "ServiceAbility::onCommand");
        
        cancelBackgroundRunning();
        HiLog.info(LABEL_LOG, "ServiceAbility::cancelBackgroundRunning");
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        HiLog.warn(LABEL_LOG, "ServiceAbility::onConnect");
        return super.onConnect(intent);
    }

    @Override
    public void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
        HiLog.warn(LABEL_LOG, "ServiceAbility::onDisconnect");
    }

    /**
     * 启动前台服务
     */
    private void startupForegroundService() {
        //创建通知请求 设置通知 id 为 9999
        NotificationRequest request = new NotificationRequest(9999);
        //创建普通通知
        NotificationRequest.NotificationNormalContent content =
                new NotificationRequest.NotificationNormalContent();
        //设置通知的标题和内容
        content.setTitle("Title").setText("Text");
        //创建通知内容
        NotificationRequest.NotificationContent notificationContent = new
                NotificationRequest.NotificationContent(content);
        //设置通知
        request.setContent(notificationContent);
        keepBackgroundRunning(9999, request);
        HiLog.info(LABEL_LOG, "ServiceAbility::startupForegroundService");
    }
}